# 读取json文件
from json import loads

file_path = "./src/config/"

def  get_Node_data():
    try:
        with open(file_path+"Node.json", "r") as file:
            data = file.read()
            json_data = loads(data)
            return json_data
    except FileNotFoundError:
        print("文件不存在")
    except Exception as e:
        print("发生错误：", str(e))

def  get_Mulit_data():
    try:
        with open(file_path+"Mulit.json", "r") as file:
            data = file.read()
            json_data = loads(data)
            return json_data
    except FileNotFoundError:
        print("文件不存在")
    except Exception as e:
        print("发生错误：", str(e))

def  get_Master_data():
    try:
        with open(file_path+"Master.json", "r") as file:
            data = file.read()
            json_data = loads(data)
            return json_data
    except FileNotFoundError:
        print("文件不存在")
    except Exception as e:
        print("发生错误：", str(e))

def get_all_host_json_data():
    result = {}
    
    try:
        Mulit = get_Mulit_data()
        for value in Mulit.values():
            host = value["host"]
            for pvname in value["MC"].keys():
                if pvname not in result:  # 避免覆盖
                    result[pvname] = host
    except Exception as e:
        print(f"Error processing Mulit data: {e}")
    
    try:
        Master = get_Master_data()
        for value in Master.values():
            host = value["host"]
            for pvname in value["MC"].keys():
                if pvname not in result:  # 避免覆盖
                    result[pvname] = host
    except Exception as e:
        print(f"Error processing Master data: {e}")
    
    try:
        Node = get_Node_data()
        for key, value in Node.items():
            result[key] = value
    except Exception as e:
        print(f"Error processing Node data: {e}")
    
    return result

def get_all_type_json_data():
    result = {}
    Mulit = get_Mulit_data()
    for value in Mulit.values():
        for pvname in value["MC"].keys():
            result[pvname] = "Mulit"

    Master = get_Master_data()
    for value in Master.values():
        for pvname in value["MC"].keys():
            result[pvname] = "Master"
    Node = get_Node_data()
    for key,value in Node.items():
        result[key] = "Node"
    return result

def get_all_pvname_data():
    result = []
    Mulit = get_Mulit_data()
    for value in Mulit.values():
        for pvname in value["MC"].keys():
            result.append(pvname)
    Master = get_Master_data()
    for value in Master.values():
        for pvname in value["MC"].keys():
            result.append(pvname)
    Node = get_Node_data()
    for pvname in Node.keys():
        result.append(pvname)
    return result

def get_MC_data():
    result = {}
    Mulit = get_Mulit_data()
    for value in Mulit.values():
        for pvname,MC in value["MC"].items():
            result[pvname] = MC
    Master = get_Master_data()
    for value in Master.values():
        for pvname,MC in value["MC"].items():
            result[pvname] = MC
    return result

def get_Master_ip_data():
    result = {}
    Master = list(get_Master_data().values())
    result["M1 ip"] = Master[0]["host"]
    result["M2 ip"] = Master[1]["host"]

    return result
Master_raw_data = get_Master_data()
Mulit_raw_data = get_Mulit_data()
Node_raw_data = get_Node_data()
Device_pvname_data = get_all_pvname_data()
Device_host_data = get_all_host_json_data()
Device_type_data = get_all_type_json_data()
Device_MC_data = get_MC_data()
Master_ip_data = get_Master_ip_data()

